package com.example.vertification.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.vertification.Utils.RedisUtil;
import com.example.vertification.entity.*;
import com.example.vertification.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@RestController
@Api(value = "积分兑换", tags = "积分兑换")
public class CartController {

    private final UserService userService;
    private final CartService cartService;
    private final OrderService orderService;
    private  final UserAddressService userAddressService;


    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    public CartController(UserService userService,CartService cartService,OrderService orderService,UserAddressService userAddressService) {
        this.userService = userService;
        this.cartService=cartService;
        this.orderService=orderService;
        this.userAddressService=userAddressService;
    }

    @GetMapping("/cart_exchange")
    @ApiOperation(value = "兑换商品，传入类型为对象的token,商品的ID号")
    public ResponseEntity<?> cart_exchange(@RequestParam String token,@RequestParam long cartid) throws IOException {
        User u = userService.getUserByToken(token);
        if(u==null){
            return ResponseEntity.ok("请先登录");
        }
        CartItem item = cartService.getCartItemByID(cartid);

        if(item==null){
            return ResponseEntity.ok("该商品不存在!");
        }
        if(u.getScore()<item.getScore()){
            return ResponseEntity.ok("积分不足,请继续努力!");
        }

        UserAddress ua = userAddressService.getEntityByUid(u.getUid());
        if(ua.getAddresses()==null || ua.getAddresses().trim().equalsIgnoreCase("")){
            return ResponseEntity.ok("请预先设置至少1条收货地址!");
        }
        JSONArray us_list = JSONUtil.parseArray(ua.getAddresses());
        if(us_list==null || us_list.size()==0){
            return ResponseEntity.ok("请预先设置至少1条收货地址!");
        }
        JSONObject us_address_first_obj = (JSONObject) us_list.get(0);
        //获取用户信息
        String address = us_address_first_obj.getStr("address");
        String name = us_address_first_obj.getStr("name");
        String telephone = us_address_first_obj.getStr("telephone");

        //新建订单+保存
        UserCartItems ci = new UserCartItems();
        ci.setAddresses(address);
        ci.setName(name);
        ci.setTelephone(telephone);
        ci.setTimestamp(new Date().getTime());
        ci.setCartID(cartid);
        ci.setUid(u.getUid());
        cartService.saveUserCartItem(ci);
        //扣款
        int intValue = (int) item.getScore(); // 显式类型转换
        u.setScore(u.getScore()-intValue);
        userService.saveUser(u);
        return ResponseEntity.ok("兑换成功");
    }


    @GetMapping("/cart_list")
    @ApiOperation(value = "商品列表，无需任何参数")
    public ResponseEntity<?> cart_list() throws IOException {
        return ResponseEntity.ok(cartService.getAllCarts());
    }


    @GetMapping("/cart_my_exchange_list")
    @ApiOperation(value = "传入用户的token，获取到已兑换的列表")
    public ResponseEntity<?> cart_my_exchange_list(@RequestParam String token) throws IOException {
        User u = userService.getUserByToken(token);
        if(u==null){
            return ResponseEntity.ok("请先登录");
        }
        List<UserCartItems> list = cartService.getAllCartsByUserID(u.getUid());
        if (list==null ||list.size()==0) {
            return ResponseEntity.ok("当前暂无兑换,请继续努力");
        }
        JSONArray ja = JSONUtil.parseArray(list);
        for(int i =0;i<ja.size();i++){
            JSONObject obj = (JSONObject) ja.get(i);
            obj.set("cartItem",cartService.getCartItemByID(obj.getLong("cartID")));
            ja.set(i,obj);
        }
        return ResponseEntity.ok(ja);
    }



}
